package com.example.treehole.fragmnet;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.treehole.R;
import com.example.treehole.activity.DynamicDetailActivity;
import com.example.treehole.activity.PublishActivity;
import com.example.treehole.adapter.ViewPage2Adapter;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class fragment1 extends BaseFragment {


    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.tab_layout)
    SegmentTabLayout tabLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.view_page)
    ViewPager2 viewPage;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.iv_filter)
    ImageView ivFilter;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.lv)
    LinearLayout lv;

    PopupWindow popupWindow;
    RadioGroup radioGroup;
    ViewPage2Adapter adapter;

    public static fragment1 newInstance() {
        return new fragment1();
    }

    private final List<String> mTitles = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();


    @Override
    protected int initLayout() {
        return R.layout.fragmen1;
    }

    @Override
    protected void initData(View view) {
        mTitles.add(getString(R.string.fragment_one_child_one));
        mTitles.add(getString(R.string.fragment_one_child_two));
        for (int i = 0; i < mTitles.size(); i++) {
            mFragments.add(fragment1Child.newInstance(i, 0));
        }
        tabLayout.setTabData(mTitles.toArray(new String[0]));
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        adapter = new ViewPage2Adapter(requireActivity(), mFragments);
        viewPage.setAdapter(adapter);
        viewPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                tabLayout.setCurrentTab(position);
            }
        });

        initPopupWindow();
    }

    private void initPopupWindow() {
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.popup_category_select, null);
        popupWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        radioGroup = layout.findViewById(R.id.rg_category);
    }

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.iv_filter,R.id.ib_publish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_filter:
                ivFilter.animate().rotation(-90);
                showPopupWindow();
                break;
            case R.id.ib_publish:
                Intent intent = new Intent(getActivity(), PublishActivity.class);
                startActivityForResult(intent,200);
        }
    }

    private void showPopupWindow() {
        //显出布局
        popupWindow.showAsDropDown(lv);
        //设置按钮切换监听事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //修改fragment参数，即selectId
                int idx = group.findViewById(R.id.rb1).getId();
                int selectId = 5-(group.getCheckedRadioButtonId() - idx);
                if (selectId==5)
                    selectId=0;
                mFragments = new ArrayList<>();
                for (int i = 0; i < mTitles.size(); i++) {
                    mFragments.add(fragment1Child.newInstance(i, selectId));
                }
                adapter.setFragments(mFragments);
                viewPage.setAdapter(adapter);
                popupWindow.dismiss();
            }
        });
        //弹窗消失监听事件
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivFilter.animate().rotation(0);
            }
        });
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

}
